package com.hxx.day8;

import org.junit.Test;

import java.util.List;
import java.util.Optional;

/**
 * 排序
 */
public class TestDemo5 {
    /**
     * 排序
     */
    @Test
    public void test1(){
        List.of(3,1,6,2,5)
                .stream()
                .sorted((x,y)->y-x)
                .forEach(System.out::println);
    }
    /**
     * 按照图书的价格排序
     */
    @Test
    public void test2(){
        List.of(new Book(1,"java",500,"leo"),
                new Book(2,"mysql",200,"monic"),
                new Book(3,"spring",300,"tony"))
                .stream()
                .sorted((x,y)->Double.compare(y.getPrice(),x.getPrice()))
                .forEach(System.out::println);
    }

    /**
     * 找到价格最高的图书
     */
    @Test
    public void test3(){
        Optional<Book> max = List.of(new Book(1, "java", 500, "leo"),
                        new Book(2, "mysql", 200, "monic"),
                        new Book(3, "spring", 300, "tony"))
                .stream()
                .max((x, y) -> Double.compare(x.getPrice(), y.getPrice()));
        // 打印最大的图书
        System.out.println(max.get());
    }
    @Test
    public void test4(){
       List.of(new Book(1, "java", 500, "leo"),
                        new Book(2, "mysql", 200, "monic"),
                        new Book(3, "spring", 300, "tony"))
               .stream()
               .map(Book::getPrice)
               .min(Double::compare)
               .ifPresent(System.out::println);
    }
    @Test
    public void test5(){
        List.of(1,2,3,4,5)
                .stream()
                .min(Integer::compare)
                .ifPresent(System.out::println);
    }
}




























